package store.lunangangster.bpmn.converter;

import javax.xml.stream.XMLStreamReader;
import store.lunangangster.bpmn.converter.parser.MessageEventDefinitionParser;
import store.lunangangster.bpmn.model.BpmnModel;
import store.lunangangster.bpmn.model.StartEvent;
import store.lunangangster.bpmn.util.BpmnXmlUtil;

/**
 * 用于startEvent的转换器，详情请见{@link StartEvent}
 *
 * @author cwk
 * @version 1.0
 */
public class StartEventConverter extends EventConverter {

  public StartEventConverter() {
    addChildElementParser(new MessageEventDefinitionParser());
  }

  /**
   * 从xsr中解析出StartEvent对象
   * @param xsr       xml输入流
   */
  @Override
  protected StartEvent convertToElement(XMLStreamReader xsr, BpmnModel model) throws Exception {
    StartEvent startEvent = null;
    startEvent = new StartEvent();
    // default is true
    boolean isInterrupting = true;
    String isInterruptingValue = xsr.getAttributeValue(null, ATTRIBUTE_EVENT_START_INTERRUPTING);
    if (ATTRIBUTE_VALUE_FALSE.equals(isInterruptingValue)) {
      isInterrupting = false;
    }
    startEvent.setInterrupting(isInterrupting);
    BpmnXmlUtil.addXmlLocation(xsr, startEvent);

    parseChildElement(xsr, startEvent, getTargetName(), model);
    return startEvent;
  }

  /**
   * 获取当前元素解析器的作用元素名
   * @return 待解析元素名
   */
  @Override
  public String getTargetName() {
    return ELEMENT_EVENT_START;
  }
}
